home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 19.zip / BS1 part 19 / AmigaLibDisk 162.adf / CLI_Utilities / XChar / xchar.c < prev   
C/C++ Source or Header  |  1988-10-02  |  4KB  |  117 lines

  1. /* xchar.c
  2.  * This program was written by Vic Parkerson using the Aztec Manx
  3.  * C compiler V3.40a on the Amega .  It is in the public domain.
  4.  * It is an example of a program to reads one ASCII file and writes
  5.  * another file.   Only characters with ASCII values above 126, and
  6.  * less than 32, except for linefeed, are stored in the new file.
  7.  * This program is intended to be run from the CLI.
  8.  *                                     Vic Parkerson
  9.  *                                     2607 Hillrise Drive
  10.  *                                     Las Cruces, N.M. 88001
  11.  * 12-24-87
  12.  * Must compile using +L, link with -lc32 . */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16.  
  17. #define CR 13
  18. #define LF 10
  19.  
  20. main( argc, argv)
  21. int argc;
  22. char **argv;
  23. {
  24.         int end;           /* fread returns zero when end of file */
  25.         int err, err1;     /* fclose return zero if ok */
  26.         int letter;        /* reply to prompt */
  27.         FILE *fp1, *fp2;   /* file pointers, zero if fopen fails */
  28.         char ch;           /* the input, checked and output character */
  29.         char i;            /* extra variable to store charrage return */
  30.  
  31.         printf("Purpose:\n");
  32.         printf("\n   An input file is read one character at a time,");
  33.         printf("\n   all characters greater than ASCII value 31 and\n");
  34.         printf("   less than 127, are stored in a new file.\n");
  35.         printf("   Linefeeds are also passed to the new file.\n\n");
  36.         printf("   One useage would be to filter out binary trash which\n");
  37.         printf("   might be caused by noise on a phone line while using\n");
  38.         printf("   a modem.\n\n");
  39.         printf("   This has been used to fix a file, so that it can\n");
  40.         printf("   then be loaded into Ed.\n\n");
  41.  
  42. /* Must have arguments to run */
  43.  
  44.         if (argc != 3) {
  45.            printf("\nTo run program type: xchar infilename outfilename\n");
  46.            exit();
  47.            }
  48.  
  49. /* Open input file */
  50.  
  51.         fp1 = fopen(argv[1], "r");
  52.         if (fp1 == 0) {
  53.             printf("\nUnable to open file %s.",argv[1]);
  54.             printf("  File or device not found.\n");
  55.             exit();
  56.             }
  57.         printf("File %s open for read.\n",argv[1]);
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. /* open output file */
  68.  
  69.         fp2 = fopen(argv[2], "x");
  70.         if (fp2 == 0) {
  71.             printf("\nUnable to open file %s.",argv[2]);
  72.             printf("  File may already exist.\n");
  73.             printf("Write over existing file(Y/N)? ");
  74.             letter = getchar();
  75.             i = getchar();        /* clear the Charrage Return */
  76.             if (letter == 89)
  77.                 letter = 'y';     /* convert to lower case y */
  78.             if (letter == 'y') {  /* if not a "y" then quit */
  79.                 putc('Y');
  80.                 fp2 = fopen(argv[2], "w");
  81.                 if (fp2 == 0) {
  82.                     printf("\nStill unable to open file!\n");
  83.                     exit();
  84.                     }
  85.                 }
  86.             else
  87.                 exit();
  88.             }
  89.         printf("\nFile %s open for write.\n",argv[2]);
  90.  
  91. /* transfer characters with filtering */
  92.  
  93.         end = 1;
  94.         printf("\n\nWorking...\n");
  95.         while (end > 0)  {
  96.             end = fread(&ch, 1, 1, fp1); /* (buffer,size,count,stream) */
  97.             if (end > 0) {
  98.                 if ((ch < 127) && (ch > 31))
  99.                     fwrite(&ch, 1, 1, fp2);
  100.                 if (ch == 10) /* Linefeed */
  101.                     fwrite(&ch, 1, 1, fp2);
  102.                 }
  103.             }
  104.  
  105. /* close files */
  106.  
  107.         err = fclose(fp2);
  108.         if (err != 0) 
  109.             printf("\nerror on write file close...");
  110.         err1 = fclose(fp1);
  111.         if (err1 != 0)
  112.             printf("\nerror on read file close...");
  113.         if (err + err1 == 0)
  114.             printf("\nTermination normal.\n");
  115. }
  116.  
  117.